<template>
  <div class="pp">
    <el-card
      class="box-card"
      style="width: 200px"
      v-for="item in imageList"
      :key="item.id"
    >
      <img
        :src="imageUrl + '/' + item.slideshowUrl"
        class="image"
        style="width: 200px"
      />
      <hr />
      <div style="display: flex; justify-content: center">
        <span style=""
          ><el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleOpen(item.id)"
          ></el-button
        ></span>
        <span style=""
          ><el-button
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(item.id)"
          ></el-button
        ></span>
      </div>
    </el-card>

    <el-card class="box-card add" style="width: 200px">
      <span style="text-align: center"
        ><el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="handleOpen(0)"
        ></el-button
      ></span>
    </el-card>

    <!-- 修改轮播图的模态框 -->
    <el-dialog title="修改轮播图图片" :visible.sync="dialogVisible" width="30%">
      <!-- 上传图片 -->
      <el-upload
        class="upload-demo"
        :action="uploadurl + imageId"
        :auto-upload="false"
        ref="upload"
        list-type="picture"
        :on-success="success"
        :limit="1"
        :on-exceed="imageBeyond"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>

      <!-- 模态框操作按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upload">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSlidenshow, deleteSlidenshowById } from "../api/public";
import config from '../config/config';
export default {
  mounted() {
    getSlidenshow().then(({ data }) => {
      this.imageList = data.data;
    });
  },
  data() {
    return {
      dialogVisible: false,
      imageList: [],
      imageId: 0,
    };
  },
  methods: {
    handleDelete(id) {
      deleteSlidenshowById(id).then(({ data }) => {
        this.$message({
          type: "success",
          message: data.msg,
        });
        getSlidenshow().then(({ data }) => {
          this.imageList = data.data;
        });
      });
    },
    success() {
      getSlidenshow().then(({ data }) => {
        this.imageList = data.data;
      });
    },
    imageBeyond() {
      this.$message.error("只能上传一张图片");
    },
    // 打开模态框
    handleOpen(id) {
      this.imageId = id;
      this.dialogVisible = true;
    },
    upload() {
      this.$refs.upload.submit();
      this.dialogVisible = false;
    },
  },
  computed: {
    // 上传图片的地址
    uploadurl() {
      let baseUrl = config.baseURL;
      let uploadurl = "/File/UploadImageById/";
      return baseUrl + uploadurl;
    },
    // 图片访问地址
    imageUrl(){
      let baseURL = config.baseURL
      return baseURL
    }
  },
};
</script>
<style scoped>
.box-card{
  margin-right: 20px;
}
.add {
  display: flex;
  align-items: center;
  justify-content: center;
}
.pp {
  /* 让他们变成一个容器（块） */
  display: flex;
  /* 横着排序（放） */
  flex-direction: row;
  /* 内容中间对齐方式小间隙 */
  /* justify-content: space-around; */
  flex-wrap: wrap;
}
</style>